package com.zzf.eduorder.controller;


import com.zzf.commonutils.R;
import com.zzf.eduorder.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-22
 */
@CrossOrigin
@RestController
@RequestMapping("/eduorder/paylog")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    //生成支付二维码
    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        Map map = payLogService.createNative(orderNo);
        System.out.println("*****返回二维码map集合:" + map);
        return R.ok().data(map);
    }

    //查询订单支付状态
    @GetMapping("querypayStatus/{orderNo}")
    public R querypayStatus(@PathVariable String orderNo) {

        Map<String, String> map = payLogService.querypayStatus(orderNo);

        System.out.println("******查询订单状态map集合:" + map);

        if (map == null) {
            return R.error().message("支付失败");
        }
        if (map.get("trade_state").equals("SUCCESS")) {//如果成功
            //更改订单状态
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中");
    }
}

